﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class User_Point_TixianController : UserController
    {
        protected Userconfig userConfig;
        protected Sysconfig sysConfig;
        public IApplyService _apply { get; set; }
        [Authorize]
        [HttpGet]
        public IActionResult Get(decimal point)
        {
            //检查用户是否登录
            var model = GetUserInfo();
            if (model == null)
            {
                return Ok(new { status = 0, msg = "温馨提示：用户尚未登录或已超时！" });
            }
            point = Math.Floor(point / userConfig.tixian_base) * userConfig.tixian_base;
            if (userConfig.tixian_switch == 0)
            {
                return Ok(new { status = 0, msg = "温馨提示：提现功能关闭！" });
            }
            #region 工作日
            string week = DateTime.Today.DayOfWeek.ToString();
            switch (week)
            {
                case "Monday":
                    week = "星期一";
                    break;
                case "Tuesday":
                    week = "星期二";
                    break;
                case "Wednesday":
                    week = "星期三";
                    break;
                case "Thursday":
                    week = "星期四";
                    break;
                case "Friday":
                    week = "星期五";
                    break;
                case "Saturday":
                    week = "星期六";
                    break;
                case "Sunday":
                    week = "星期日";
                    break;
            }
            #endregion
            if (!userConfig.tixian_week.Contains(week))
            {
                return Ok(new { status = 0, msg = "温馨提示：" + week + "不可提现,仅" + userConfig.tixian_week + "可提！" });
            }
            if (point < userConfig.tixian_start)
            {
                return Ok(new { status = 0, msg = "温馨提示：请输入正确的最少" + userConfig.tixian_start + "提现金额(" + userConfig.tixian_base + "的倍数)！" });
            }
            var apply = _apply.GetFirst(model.id);
            if (apply != null)
            {
                return Ok(new { status = 0, msg = "温馨提示：提现申请中！" });
            }
            if (apply != null)
            {
                if (Utils.IsInSameWeek(apply.add_time, DateTime.Now))
                {
                    return Ok(new { status = 0, msg = "温馨提示：每周只能提一次！" });
                }
            }
            //银行信息
            if (string.IsNullOrEmpty(model.bank_name))
            {
                return Ok(new { status = 0, msg = "温馨提示：请完善提现账户！" });
            }
            //银行信息
            if (string.IsNullOrEmpty(model.bank_number))
            {
                return Ok(new { status = 0, msg = "温馨提示：请完善卡号！" });
            }
            //提现
            if (model.cash_point < point)//现金积分
            {
                return Ok(new { status = 0, msg = "温馨提示：金额不足！" });
            }
            //生成申请记录
            Apply ap = new Apply();
            ap.user_id = model.id;
            ap.apply_point = point;
            bool flag = _apply.Add(ap);
            if (!flag)
                return Ok(new { status = 0, msg = "温馨提示：提现失败！" });
            //保存成功后返回
            return Ok(new { status = 1, msg = "提现申请成功" });
        }
    }
}